﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

namespace App
{
    public partial class TeamList : System.Web.UI.Page
    {

        public int state;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Common.GetLoggedUser(this.Page) == null)
            {
                Response.Redirect("Login.aspx");
                return;
            }
            if (MidApp.ManageIsAdministrator.IsAdministrator(Common.GetLoggedUser(this.Page).UserID))
            {
                Response.Redirect("Administration.aspx");
                return;
            }    

            int.TryParse(Request.Cookies["FindingState"].Value, out state);
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            GetDataContent();
            SetDataContextForTextBoxSearch();
        }

        private void SetDataContextForTextBoxSearch()
        {
            switch (DropDownListFilter.SelectedIndex)
            {
                case 0:
                    AutoCompleteExtender1.ServiceMethod = "GetTeamNames";
                    break;
                case 1:
                    AutoCompleteExtender1.ServiceMethod = "GetTeamLocations";
                    break;
                case 2:
                    AutoCompleteExtender1.ServiceMethod = "GetTeamLeaders";
                    break;
                default:
                    break;
            }
        }

        protected void IsEmptyRepeator()
        {
            ICollection list = RepeaterTeams.DataSource as ICollection;

            bool isEmpty = list.Count == 0 ? true : false;

            if (isEmpty)
            {
                lblEmptyRepeaterText.Visible = true;
            }
            else
            {
                lblEmptyRepeaterText.Visible = false;
            }
        }

        protected void LinkButtonJoin_Click(object sender, EventArgs e)
        {
            MidApp.ManagePendingRequest.ApplyUserToTeam(Common.GetLoggedUser(this.Page).UserID, int.Parse(((LinkButton)sender).CommandArgument));
            LabelInfo.Text = "Byla podána žádost o přijetí do týmu";
            LabelInfo.Visible = true;
            //((LinkButton)sender).Visible = false;

        }
        //obsluha pro presmerovani na detail tymu
        protected void LinkButtonTeamInfo_Click(object sender, EventArgs e)
        {
            Response.Cookies["TeamDetail"].Value = ((LinkButton)sender).CommandArgument;
            Response.Redirect("TeamDetails.aspx");
        }

        protected bool IsRelatedToTeam(object sender)
        {
            bool aux=true;
            
            
            if (!MidApp.ManageIsMember.IsTeamMember(((AppLib.Team)sender).TeamID, Common.GetLoggedUser(this.Page).UserID)){
                aux = false;
            }

            if (!MidApp.ManagePendingRequest.IsTeamApplicant(((AppLib.Team)sender).TeamID, Common.GetLoggedUser(this.Page).UserID))
            {
                aux = false;
            }

            return aux; 
                
        }
        protected string GetLinkButtonDescription(object sender)
        {

            if (!MidApp.ManageIsMember.IsTeamMember(((AppLib.Team)sender).TeamID, Common.GetLoggedUser(this.Page).UserID))
            {
                return "Jsem členem";
            }

            if (!MidApp.ManagePendingRequest.IsTeamApplicant(((AppLib.Team)sender).TeamID, Common.GetLoggedUser(this.Page).UserID))
            {
                return "Zažadáno";
            }

            return "Přidat se";
        }

        protected string AssignTeamAvatar(object sender)
        {
            string aux = ((AppLib.Team)sender).ImageURL;
            return aux;
        }
        protected string AssignTeamID(object sender)
        {
            string aux = ((AppLib.Team)sender).TeamID.ToString();
            return aux;
        }

        //vrati nick leadera podle jeho id
        protected string GetLeaderNick(object sender)
        {
            var aux = ((AppLib.Team)sender).LeaderID;

            return MidApp.ManageUser.GetUserByID(aux).Nick;
        }
        //obsluha tlacitka pro zorbazeni detailu o leaderovi (zakladateli) tymu
        protected void LinkButtonUserInfo_Click(object sender, EventArgs e)
        {
            Response.Cookies["UserDetail"].Value = ((LinkButton)sender).CommandArgument;
            Response.Redirect("UserDetail.aspx");
        }

        protected void ButtonFilter_Click(object sender, EventArgs e)
        {
            Response.Cookies["FindingState"].Value = DropDownListFilter.SelectedIndex.ToString();
            state = DropDownListFilter.SelectedIndex;
            GetDataContent();
            IsEmptyRepeator();
            
        }

        private void GetDataContent()
        {
            
            switch (state)
            {
                case 0:
                    RepeaterTeams.DataSource = MidApp.ManageTeam.GetTeamListOfTeamsContainingTeamName(TextBoxSearch.Text);
                    RepeaterTeams.DataBind();                    
                    break;
                case 1:
                    RepeaterTeams.DataSource = MidApp.ManageTeam.GetTeamListOfTeamsContainingLocation(TextBoxSearch.Text);
                    RepeaterTeams.DataBind();
                    break;
                case 2:
                    RepeaterTeams.DataSource = MidApp.ManageTeam.GetTeamListOfTeamsContainingFounder(TextBoxSearch.Text);
                    RepeaterTeams.DataBind();
                    break;
                default:                    
                    //RepeaterTeams.DataSource = MidApp.ManageTeam.GetAllActiveTeamsOrderedByTeamName();
                    //RepeaterTeams.DataBind();
                    break;
            }
        }
        [System.Web.Services.WebMethod]
        [System.Web.Script.Services.ScriptMethod]
        public static string[] GetTeamLeaders(string prefixText, int count)
        {
            return MidApp.ManageTeam.GetAllTeamLeaders(MidApp.ManageTeam.GetAllActiveTeamsOrderedByTeamName(), prefixText).ToArray();
        }

        [System.Web.Services.WebMethod]
        [System.Web.Script.Services.ScriptMethod]
        public static string[] GetTeamNames(string prefixText, int count)
        {
            return MidApp.ManageTeam.GetAllTeamNames(MidApp.ManageTeam.GetAllActiveTeamsOrderedByTeamName(), prefixText).ToArray();                        
        }

        [System.Web.Services.WebMethod]
        [System.Web.Script.Services.ScriptMethod]
        public static string[] GetTeamLocations(string prefixText, int count)
        {                 
            return MidApp.ManageTeam.GetAllTeamLocations(MidApp.ManageTeam.GetAllActiveTeamsOrderedByTeamName(), prefixText).ToArray();           
        }
    }
}